//
//  MBGoodsViewModel.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/23.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBGoodsViewModel.h"
#import "MBGoodsNetTool.h"
#import "MBGoodsModel.h"

@interface MBGoodsViewModel ()
@property (nonatomic,strong) MBSellerGoodsParam* param;

@end

@implementation MBGoodsViewModel

-(void)setUpData
{
    [super setUpData];
    
    self.is_on_sale = 1;
}

-(void)sendRequest:(MBRequestResult)result
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    self.param.p = self.currentPage;
    self.param.is_on_sale = self.is_on_sale;
    self.param.store_id = userModel.store_id;
    self.param.token = userModel.token;
    [MBGoodsNetTool netToolPostSellerGoodsParam:self.param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            NSArray* array = response.result;
            NSMutableArray* list = [NSMutableArray array];
            
            for (int i=0; i<array.count; i++) {
                MBGoodsModel* model = [MBGoodsModel yy_modelWithDictionary:array[i]];
                model.is_on_sale = self.is_on_sale;
                [list addObject:model];
            }
            [self factoryInitDataModelArray:list totalCount:10 cellViewModelClass:[MBGoodsTableViewCellModel class]];
            
            result(YES);
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
            result(NO);
        }
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"获取商品失败，请稍后重试！"];
        result(NO);
    }];
}

-(void)toGoodsDetailVCWithViewModel:(MBGoodsTableViewCellModel*)cellModel
{
    MBLoginUserModel* userModel = [MBLoginUserModelArchieveTool userInfoUnAchieveFromFile];
    MBGoodsInfoParam* param = [[MBGoodsInfoParam alloc]init];
    param.goodsId = cellModel.dataModel.goods_id;
    param.token = userModel.token;
    [MBGoodsNetTool netToolGoodsInfoWithParam:param success:^(MBHTTPResponse *response) {
        if (response.status == 1) {
            MBGoodsInfoModel* goodsInfo = [MBGoodsInfoModel yy_modelWithJSON:response.result];
            self.selectInfoModel = goodsInfo;
        }else {
            [InterfaceHUD showErrorHUDWithString:response.msg];
        }
    } failure:^(NSError *error) {
        [InterfaceHUD showErrorHUDWithString:@"获取商品详情失败，请稍后重试！"];
    }];

}

-(void)setIs_on_sale:(int)is_on_sale
{
    _is_on_sale = is_on_sale;
    
    [self initRequestPullPage];
}

-(MBSellerGoodsParam *)param
{
    if (_param == nil) {
        _param = [[MBSellerGoodsParam alloc]init];
    }
    return _param;
}

- (NSInteger)numberOfRowInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSections
{
    return self.dataArray.count;
}

- (MBBaseTableCellViewModel *)cellViewModelForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MBBaseTableCellViewModel* viewModel = self.dataArray[indexPath.section];
    return viewModel;
}

- (CGFloat)heightForHeaderInSection:(NSInteger)section
{
    return [UIView lf_sizeFromIphone6:10];
}

@end
